<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="120px"
      v-loading="formLoading"
    >
      <el-form-item label="广告账户" prop="accountId">
        <el-select v-model="formData.accountId" placeholder="请选择广告账户" class="!w-50%">
          <el-option
            v-for="item in accountSimpleList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">不启用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="执行间隔(分钟)" prop="minInterval">
        <el-input-number
          v-model="formData.minInterval"
          placeholder="请输入执行间隔"
          :min="1"
          :max="100"
          class="!w-50%"
        />
      </el-form-item>
      <el-form-item label="转入位置" prop="transPos">
        <el-radio-group v-model="formData.transPos" @change="transPosChange">
          <el-radio :label="1">线索列表</el-radio>
          <el-radio :label="2">线索公海</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="当线索负责人为空或不匹配时"
        prop="ownerEmptyHandle"
        v-if="formData.transPos == 1"
        label-width="220"
        label-position="top"
      >
        <el-radio-group v-model="formData.ownerEmptyHandle">
          <el-radio :label="0">跳过</el-radio>
          <el-radio :label="2">指定的用户id</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="线索公海" prop="pooId" v-if="formData.transPos == 2">
        <el-select v-model="formData.pooId" placeholder="请选择公海" class="!w-50%">
          <el-option
            v-for="item in cluePoolList"
            :key="item.id"
            :label="item.poolName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import * as ClueTransRuleApi from '@/api/crm/cluetransrule'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  accountId: undefined,
  status: undefined,
  minInterval: undefined,
  transPos: undefined,
  ownerEmptyHandle: undefined,
  pooId: undefined
})
const formRules = reactive({
  accountId: [
    {
      required: true,
      message: '账户ID不能为空',
      trigger: 'blur'
    }
  ],
  status: [
    {
      required: true,
      message: '状态不能为空',
      trigger: 'blur'
    }
  ],
  minInterval: [
    {
      required: true,
      message: '执行间隔不能为空',
      trigger: 'blur'
    }
  ],
  transPos: [
    {
      required: true,
      message: '转入位置不能为空',
      trigger: 'blur'
    }
  ],
  ownerEmptyHandle: [
    {
      required: true,
      message: '负责人空时处理不能为空',
      trigger: 'blur'
    }
  ]
})
const formRef = ref() // 表单 Ref
const accountSimpleList = ref([]) // 广告账户列表
const cluePoolList = ref([]) // 公海列表
/** 打开弹窗 */
const open = async (type: string, id?: number, poolList, accountList) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  cluePoolList.value = poolList
  accountSimpleList.value = accountList
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ClueTransRuleApi.getClueTransRule(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

// 转入位置切换时
const transPosChange = (val) => {
  if (val == 1) {
    formData.value.pooId = 0
  } else if (val == 2) {
    formData.value.ownerEmptyHandle = 0
    formData.value.pooId = undefined
  }
}

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ClueTransRuleApi.ClueTransRuleVO
    if (formType.value === 'create') {
      await ClueTransRuleApi.createClueTransRule(data)
      message.success(t('common.createSuccess'))
    } else {
      await ClueTransRuleApi.updateClueTransRule(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    accountId: undefined,
    status: undefined,
    minInterval: undefined,
    transPos: undefined,
    ownerEmptyHandle: undefined,
    pooId: undefined
  }
  formRef.value?.resetFields()
}
</script>
